<template>
  <div>
    <kendoForm
      :kendo-form-items="kendoFormItems"
      :kendo-form="kendoForm"
      :kendo-params-value="kendoParamsValue"
    />
  </div>
</template>

<script>
  import kendoForm from '@/Views/components/kendo-vue-component/kendoForm/index.vue'
  import { isBlank } from '@/Utils/strUtil.js'
  export default {
    name: 'FileAddCmpt',
    components: {
      kendoForm
    },
    data() {
      return {
        dialogName: this.$attrs.data.dialogName,
        kendoFormItems: [
          {
            name: 'directoryNo',
            label: '文件目录编号',
            paramName: 'directoryNo',
            required: true,
            readonly: true,
            controlType: 'input'
          },
          {
            name: 'directoryName',
            label: '文件目录名称',
            paramName: 'directoryName',
            required: true,
            controlType: 'input',
            change: this.dirNamechange
          },
          {
            name: 'parentDirectoryNo',
            label: '上级文件目录编号',
            paramName: 'parentDirectoryNo',
            readonly: true,
            controlType: 'input'
          },
          {
            name: 'directoryHier',
            label: '文件目录层级编码',
            paramName: 'directoryHier',
            required: true,
            readonly: true,
            controlType: 'input'
          },
          {
            name: 'directoryHierName',
            label: '文件目录层级名称',
            paramName: 'directoryHierName',
            required: true,
            readonly: true,
            controlType: 'input'
          }
        ],
        kendoForm: {
          ref: 'prodForm',
          valid: 'valid',
          buttons: [
            {
              name: '保存', // 按钮内容
              class: 'k-primary', // 按钮样式
              icon: 'save',
              click: this.save
            },
            {
              name: '重置', // 按钮内容
              icon: 'reset-sm',
              click: this.reset
            }
          ]
        },
        kendoParamsValue: {
          id: '',
          directoryNo: '',
          directoryName: '',
          parentDirectoryNo: '',
          directoryHier: '',
          directoryHierName: ''
        }
      }
    },
    mounted() {
      // 判断是否在根目录上创建文件或者文件夹
      const params = this.$attrs.data.param
      this.$post('/service/decoration/commonInitDirNo', params).then(res => {
        if (res && res.code === 200) {
          this.$set(this.kendoParamsValue, 'parentDirectoryNo', params.code)
          this.$set(this.kendoParamsValue, 'directoryNo', res.data)
          if (isBlank(params.dirHierNo)) {
            this.$set(this.kendoParamsValue, 'directoryHier', res.data)
          } else {
            this.$set(this.kendoParamsValue, 'directoryHier', `${params.dirHierNo}/` + res.data)
            this.$set(this.kendoParamsValue, 'directoryHierName', `${params.dirHierName}`)
          }
        } else {
          this.$KendoNotify({
            title: '错误信息:' + res.code,
            message: res.message
          }, 'error')
        }
      })
    },
    methods: {
      initDirNo() {

      },
      dirNamechange(e) {
        const value = e.sender.value()
        if (isBlank(this.kendoParamsValue.directoryHierName)) {
          this.$set(this.kendoParamsValue, 'directoryHierName', value)
        } else {
          this.$set(this.kendoParamsValue, 'directoryHierName', `${this.$attrs.data.param.dirHierName}/` + value)
        }
      },
      save(paramVal, validator) {
        if (validator.validate()) {
          this.$post('/service/decoration/commonCreateDir', paramVal).then(res => {
            if (res.code !== 200) {
              this.$KendoNotify({
                title: '错误信息:' + res.code,
                message: res.message
              }, 'error')
            } else {
              this.$KendoNotify({
                title: '提示信息:',
                message: res.message
              }, 'success')
              this.$bus.emit('dialog' + `${this.dialogName}`, true)
            }
          })
        }
      },
      reset(paramVal) {
        Object.keys(paramVal).forEach(key => {
          if (Array.isArray(paramVal[key])) {
            this.kendoParamsValue[key] = []
          } else {
            this.kendoParamsValue[key] = ''
          }
        })
      }
    }
  }
</script>

<style>

</style>
